package io.github.uicdb.et.element;

import com.mojang.serialization.Codec;
import io.github.uicdb.et.*;
import io.github.uicdb.et.register.ElementalObject;
import io.github.uicdb.et.register.IElementalGetter;
import net.minecraft.core.Holder;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public  class BasicElement implements IElementalGetter, Comparable<BasicElement> {
    public static final Codec<BasicElement> CODEC = ElementalTheory.BASIC_ELEMENT_REGISTRY.byNameCodec();
    public static final Codec<Holder<BasicElement>> HOLDER_CODEC = ElementalTheory.BASIC_ELEMENT_REGISTRY.holderByNameCodec();
    public static final StreamCodec<RegistryFriendlyByteBuf, BasicElement> STREAM_CODEC = ByteBufCodecs.registry(ElementalTheory.BASIC_ELEMENT_REGISTRY_RESOURCE_KEY);
    public static final StreamCodec<RegistryFriendlyByteBuf, Holder<BasicElement>> HOLDER_STREAM_CODEC = ByteBufCodecs.holderRegistry(ElementalTheory.BASIC_ELEMENT_REGISTRY_RESOURCE_KEY);

    private MutableComponent name = null;

    public MutableComponent getName() {
        return name == null ? (name = Component.translatable(ElementalTheory.BASIC_ELEMENT_REGISTRY.getKey(this).toLanguageKey("element"))) : name;
    }

    protected ElementalObject elementalObject;


    @Override
    public ElementalObject getElementalObject() {
        return elementalObject;
    }

    @Override
    public void setElementalObject(ElementalObject elementalObject) {
        this.elementalObject = elementalObject;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof BasicElement)) return false;
        return ElementalTheory.BASIC_ELEMENT_REGISTRY.getKey(this).equals(ElementalTheory.BASIC_ELEMENT_REGISTRY.getKey((BasicElement) obj));
    }

    @Override
    public int hashCode() {
        return Objects.hash(ElementalTheory.BASIC_ELEMENT_REGISTRY.getKey(this));
    }

    @Override
    public int compareTo(@NotNull BasicElement o) {
        return ElementalTheory.BASIC_ELEMENT_REGISTRY.getKey(this).compareTo(ElementalTheory.BASIC_ELEMENT_REGISTRY.getKey(o));
    }
}
